package com.asjy.po;

import java.util.List;

/**
 * @Auther: lilinlin
 * @Date: 2021/4/12
 * @Description: com.asjy.po
 * @version: 1.0
 */
public class PageBean<T> {
    private Integer pageNow=1;
    private Integer pageSize=12;
    private Integer pageCount=0;
    private Integer rowCount=0;
    private Integer start = 0;
    private List<T> list;
    private int[] pages;

    /**
     * 页码数组 [1,2,3,4,5,6,7]
     * 7个页码
     * @return
     */
     public int[] getPages() {
        //pageNow和pageCount的限制
        int pageNow = Math.min(getPageNow(),getPageCount());
        int pageCount = getPageCount();

        //1.设置初始值和结束值
        int begin = 1;
        int end = 1;

        //2.判断当前页的分页初始值和结束值,从而达到滚动效果
        if (pageNow < 4) {
            //若pageNow<4则begin=1。
            //若结束页pageCount>7则为7，否则pageCount本身就是end
            end = Math.min(pageCount, 7);
        }else{
            //若pageNow>4则初始值 pageNow-3
            begin = Math.min(pageNow-3,pageCount-7);
            //若结束页是pageCount则为pageCount，小于pageCount，则为pageNow+3
            end = Math.min(pageNow+3,pageCount);
        }
        int y=0;
        //4.封装分页数组
        begin = Math.max(begin,1);
        //创建分页数组
        pages = new int[end-begin+1];
        //封装数组
        for (int i=begin;i<=end;i++){
            pages[y]=i;
            y++;
        }
        return pages;
    }



    public Integer getStart() {
        return (pageNow-1)*pageSize;
    }

    public Integer getPageNow() {
        return pageNow;
    }

    public void setPageNow(Integer pageNow) {
        if(pageNow<1){
            this.pageNow=1;
        }else if(pageNow>getPageCount()){
            this.pageNow=pageCount;
        }else{
            this.pageNow = pageNow;
        }
        this.pageNow = pageNow;


    }

    public Integer getPageSize() {
        return pageSize;
    }

    public void setPageSize(Integer pageSize) {
        this.pageSize = pageSize;
    }

    public Integer getPageCount() {
        if(rowCount%pageSize==0){
            pageCount = rowCount/pageSize;
        }else{
            pageCount = rowCount/pageSize+1;
        }
        return pageCount;
    }



    public Integer getRowCount() {
        return rowCount;
    }

    public void setRowCount(Integer rowCount) {
        this.rowCount = rowCount;
    }

    public List<T> getList() {
        return list;
    }

    public void setList(List<T> list) {
        this.list = list;
    }
}